<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field.options.displayStyle"
  >
    <div class="scene-report-pick-info-widget" :style="getStyle('head')">
      <div class="pick-area-wrap">
        <div class="pick-area">
          <div class="top-header">
            <div class="left">
              <img :src="require(`@/components/assets/images/design/1-12.webp`)"/>
            </div>
            <div class="middle" :style="getStyle('content')">
              {{optionsInfo.content}}
            </div>
            <div class="right">
              <img :src="require(`@/components/assets/images/design/1-12.webp`)"/>
            </div>
          </div>

          <div class="pick-type">
            <div class="item button-wrap" v-for="(item) in getReportPickInfo" :key="item.terminalId">
              <div class="bottom">
                <div class="button" @click="handlePickType(item)">{{item.title}}</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {Icon} from 'vant';
import {pxToVw} from '@/utils/common';
import emitter from "@/utils/designer/emitter";
import {buildParentApi} from '@/utils/designer/util';
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-report-pick-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    Icon,
    CompOperate,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
    devType: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      step: 0,
      tipsShow: true,
      backcountdown: 120000,
      optionsInfo: Object(),
    }
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.field?.options?.reportPickInfoConfig) || Object();
        let Obj = Object();
        switch (val) {
          case 'head':
            Reflect.set(Obj, 'backgroundColor', `${(info.backgroundColor)}`);
            let step = (info.step);
            switch (step) {
              case 0:
                Reflect.set(Obj, 'height', pxToVw(info.headHeight));
                break
              case 1:
                let heighe = parseInt(info.headHeight) + 100;
                Reflect.set(Obj, 'height', pxToVw(heighe));
                break
              default:
                Reflect.set(Obj, 'height', pxToVw(info.headHeight));
                break
            }
            return Obj;
          case 'content':
            Reflect.set(Obj, 'color', `${(info.fontColor)}`);
            Reflect.set(Obj, 'fontSize', `${pxToVw(info.fontSize)}`);
            return Obj;
        }
      }
    },

    //步距展示对应的组件
    showComp() {
      return function (val) {
        return this.optionsInfo.step === val
      }
    },

    // 获取父级传递过来的step
    getBackcountdown() {
      let {backcountdown} = (this.field.options.reportInfoConfig)
      return backcountdown * 1000 || 120000
    },


    //  取号机业务类型列表
    getReportPickInfo() {
      let devType = (this.devType);
      let reportPickInfo = this.$store.getters['getReportPickInfo'];
      let reportPickShowInfo;
      if (devType) {
        reportPickShowInfo = reportPickInfo['apiData']
      } else {
        reportPickShowInfo = reportPickInfo['mockData']
      }
      return reportPickShowInfo;
    }
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    handlePickType(row) {
      // 调用父组件的函数--回传数据
      //this.$emit('goToInit', row);
      this.parentApi.executeParentMethod('goToInit', row);
    },

    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      const self = this;
      switch (key) {
        case "reportPickInfo":
          self.$store.dispatch('setReportPickInfo', data)
          break
      }
    },

    // 返回首页
    handleBack() {
      this.optionsInfo.step = 0;
      this.parentApi.executeParentMethod('openReadCardScan');
    },

  },

  watch: {
    // 监听父级传递过来的props--field
    field: {
      handler: function (val) {
        let key = val.options['config']
        this.optionsInfo = val.options[key];
        this.backcountdown = (this.optionsInfo.backCountdown) * 1000 || 120000;
        this.$store.dispatch('setReportStepInfo', (this.optionsInfo.step));
      },
      deep: true,
      immediate: true,
    },

    // 步-距
    'optionsInfo.step': {
      handler: function (val) {
        const self = this;
        this.optionsInfo.step = parseInt(val);
        // 步骤过程发生变化--执行时间的倒计时
        if (parseInt(val) === 1) {
          // self.backcountdown = (self.getBackcountdown);
          self.timer = setInterval(() => {
            self.backcountdown = (self.backcountdown - 1000);
            // 到达时间--返回首页-清空定时器
            if ((self.backcountdown) <= 0) {
              self.handleBack()
              clearInterval(self.timer)
            }
          }, 1000)
        } else {
          clearInterval(self.timer)
        }
      },
      deep: true,
      immediate: true,
    },
  },

  destroyed() {
    this.unregisterFromRefList();
    this.$store.dispatch('setReportStepInfo', 0);
  },

};
</script>

<style lang="scss" scoped>
.scene-report-pick-info-widget {
  font-size: 60px;
  overflow: hidden;
  background: transparent !important;
  position: relative;

  .pick-area-wrap {
    height: 100%;
    padding: 40px 30px;
    box-sizing: border-box;

    .pick-area {
      display: flex;
      height: 100%;
      padding: 166px 0;
      flex-direction: column;
      box-sizing: border-box;
      border-radius: 60px;
      overflow: hidden;
      background: white;

      .top-header {
        line-height: 84px;
        font-style: normal;
        margin-bottom: 90px;
        text-align: center;
        font-weight: 500;
        font-size: 60px;
        color: #333333;
        padding: 42px 114px;
        display: flex;
        justify-content: flex-end;

        .left {
          display: inline-block;
          width: 66px;
          height: 66px;
          align-self: center;
          position: relative;
          top: -20px;

          img {
            height: 100%;
            width: 100%;
            align-items: center;
            align-self: center;
          }
        }

        .middle {
          width: calc(100% - 66px - 66px);
          margin: 0 32px;
          font-size: 60px;
          color: #333333;
          line-height: 1.4;
          font-weight: normal;
          align-self: self-end;
          text-align: center;
          font-style: normal;
          @include fontWeight();
        }

        .right {
          display: inline-block;
          width: 66px;
          height: 66px;
          position: relative;
          top: -20px;
          align-items: center;
          align-self: center;

          img {
            height: 100%;
            width: 100%;
            align-items: center;
            align-self: center;
          }
        }

      }

      .button-wrap {
        flex-basis: 744px;
        width: 100%;
        text-align: center;

        .bottom {
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;
          padding:0 120px 60px;
          .button {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            width: 100%;
            border-radius: 100px;
            line-height: 165px;
            font-size: 80px;
            cursor: pointer;
            color: #E8E8E8;
            background: #0059B3;
            box-shadow: 0 9px 0 rgba(0, 89, 179, 0.5), 0 9px 25px rgba(0, 89, 179, 0.06);
          }
        }
      }
    }
  }
}
</style>
